package cn.icanci.ladybird.config;

import cn.icanci.ladybird.interceptor.CorsInterceptor;
import cn.icanci.ladybird.interceptor.UserLoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author: icanci
 * @date: Created in 2020/9/29 14:58
 * @classAction: 处理拦截器的配置类
 * -
 * - 不使用权限就可以访问的：/api/v1/pub/
 * - 需要使用权限访问: /api/v1/pri/ 、 /api/v1/pri/common
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Bean
    UserLoginInterceptor userLoginInterceptor() {
        return new UserLoginInterceptor();
    }

    @Bean
    CorsInterceptor corsInterceptor() {
        return new CorsInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 拦截全部，并且放行
        registry.addInterceptor(corsInterceptor()).addPathPatterns("/**");

        // 拦截用户请求
        registry.addInterceptor(userLoginInterceptor())
                // 斜杠 / 一定要加上
                .addPathPatterns("/api/v1/pri/user/*/**", "/api/v1/pri/common/*/**")
                // 不拦截的登录，只要是pub都不进行拦截，用户登录注册不拦截
                .excludePathPatterns(
                        "/api/v1/pub/*/**", "/api/v1/pri/user/do/*/**", "/api/v1/pri/search/*/**");

    }
}
